﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using FTBS.Models.DAO;
using FTBS.Models.Entities;

namespace FTBS.Models.BLO
{
    public class UserBLO
    {
        private UserDAO userDAO = new UserDAO();

        /// <summary>
        /// Check login of account and get role
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>Role Name</returns>
        /// <author>HungPH</author>
        /// <date>3/4/2014</date>
        public String GetRoleNameFromLogin(String username, String password)
        {
            String roleName = "";
            var roleId = userDAO.GetRoleIdFromLogin(username, password);
            if (roleId == 1)
            {
                roleName = "Customer";
            }
            else if (roleId == 2)
            {
                roleName = "Admin";
            }
            else if (roleId == 3)
            {
                roleName = "Staff";
            }
            else if (roleId == 4)
            {
                roleName = "Supplier";
            }
            return roleName;
        }

        /// <summary>
        /// display all staffs
        /// </summary>
        /// <returns>list users</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public List<User> DisplayAllStaffs()
        {
            var listStaffs = userDAO.LoadAllStaffs();
            return listStaffs;
        }

        /// <summary>
        /// display all customers
        /// </summary>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public List<User> DisplayAllCustomers()
        {
            var listCustomers = userDAO.LoadAllCustomers();
            return listCustomers;
        }

        /// <summary>
        /// display all ssuppliers
        /// </summary>
        /// <returns>list users</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public List<User> DisplayAllSuppliers()
        {
            var listSuppliers = userDAO.LoadAllSuppliers();
            return listSuppliers;
        }

        /// <summary>
        /// load user by username
        /// </summary>
        /// <param name="username"></param>
        /// <returns>User</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public User LoadUserByUserName(String username)
        {
            return userDAO.LoadUserByUserName(username);
        }

        /// <summary>
        /// Create a staff
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool CreateStaff(User user)
        {
            var result = userDAO.AddStaff(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Create a supplier
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool CreateSupplier(User user)
        {
            var result = userDAO.AddSupplier(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Register account
        /// </summary>
        /// <param name="customer"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        public bool Register(User customer)
        {
            var result = userDAO.Register(customer);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// edit info staff
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool EditStaff(User user)
        {
            var result = userDAO.EditStaff(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// edit info customer
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        public bool EditCustomer(User user)
        {
            var result = userDAO.EditCustomer(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// edit info supplier
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool EditSupplier(User user)
        {
            var result = userDAO.EditSupplier(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// active user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool ActiveUser(User user)
        {
            var result = userDAO.ActiveUser(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// deactive user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool DeactiveUser(User user)
        {
            var result = userDAO.DeactiveUser(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// delete user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool DeleteUser(User user)
        {
            var result = userDAO.Delete(user);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// check existed user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool CheckUsername(User user)
        {
            var result = userDAO.CheckIsExisted(user);
            return result;
        }

        /// <summary>
        /// check existed email
        /// </summary>
        /// <param name="user"></param>
        /// <returns>success: true; fail: false</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public bool CheckEmail(User user)
        {
            var result = userDAO.CheckEmail(user);
            return result;
        }

        /// <summary>
        /// change password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pass"></param>
        /// <returns>true: success; false:fail</returns>
        /// <author>hungph</author>
        public bool ChangePassword(string username, string pass)
        {
            var result = userDAO.ChangePassword(username, pass);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// send mail
        /// </summary>
        /// <param name="email"></param>
        /// <param name="username"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <author>hungph</author>
        public void SendMail(string email,string username, string subject, string body)
        {
            SmtpClient emailClient = new SmtpClient("smtp.gmail.com");
            emailClient.Port = 587;
            emailClient.EnableSsl = true;
            emailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            emailClient.Credentials = new NetworkCredential("hoanghungvt91@gmail.com", "hoanghung91");
            using (MailMessage message = new MailMessage("hoanghungvt91@gmail.com", email)
            {
                Subject = subject,
                Body = body,
                IsBodyHtml = true,
                BodyEncoding = System.Text.Encoding.UTF8
            })
            emailClient.Send(message);
        }

        /// <summary>
        /// generate MD5 string
        /// </summary>
        /// <param name="text"></param>
        /// <returns>MD5 string</returns>
        /// <author>hungph</author>
        public String GenerateMD5(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
            byte[] result = md5.Hash;
            var builder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                builder.Append(result[i].ToString("x2"));
            }
            return builder.ToString();
        }

        /// <summary>
        /// generate random string
        /// </summary>
        /// <param name="size"></param>
        /// <returns>random string</returns>
        /// <author>hungph</author>
        public string RandomString(int size)
        {
            try
            {
                var random = new Random((int)DateTime.Now.Ticks);
                var builder = new StringBuilder();
                for (var i = 0; i < size; i++)
                {
                    var nextDouble = random.NextDouble();
                    char ch;
                    if (nextDouble <= 0.5)
                    {
                        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(18 * nextDouble + 48)));
                    }
                    else
                    {
                        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    }
                    builder.Append(ch);
                }
                return builder.ToString();
            }
            catch (Exception)
            {
                return "";
            }

        }
        public bool IsUsed(User user) {
            return userDAO.IsUsed(user);
        }

        /// <summary>
        /// get status of user
        /// </summary>
        /// <param name="username"></param>
        /// <returns>status of user</returns>
        /// <author>hungph</author>
        public bool GetStatusOfUser(string username)
        {
            return userDAO.CheckStatusOfUser(username);
        }
    }
}